package com.hlk.user.controller;

import com.hlk.user.UserControllerApi;
import com.hlk.enums.ResponseStatusEnum;
import com.hlk.exception.HuaException;
import com.hlk.pojo.AppUser;
import com.hlk.pojo.bo.UpdateUserInfoBO;
import com.hlk.pojo.vo.AppUserVO;
import com.hlk.pojo.vo.UserAccountInfoVO;
import com.hlk.result.Result;
import com.hlk.user.service.UserService;
import com.hlk.utils.JsonUtils;
import com.hlk.utils.RedisCountUtils;
import com.hlk.utils.ValidateUtils;
import com.hlk.utils.redis.RedisKeys;
import com.hlk.utils.redis.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author hlk
 * @version 1.0
 * @date 2020-12-21 10:25
 */
@RestController
public class UserController implements UserControllerApi {
    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisCountUtils redisCountUtils;

    @Override
    public Result getUserInfo(String userId) {
        if (StringUtils.isBlank(userId)) {
            throw new HuaException(ResponseStatusEnum.UN_LOGIN);
        }

        AppUser user = getUser(userId);

        AppUserVO appUserVO = new AppUserVO();
        BeanUtils.copyProperties(user, appUserVO);

        // 获取粉丝数
        appUserVO.setMyFansCounts(redisCountUtils.getCountFromRedis(RedisKeys.getWriterFansCountKey(userId)));
        appUserVO.setMyFollowCounts(redisCountUtils.getCountFromRedis(RedisKeys.getMyFollowCountKey(userId)));


        return Result.ok(appUserVO);
    }


    @Override
    public Result updateUserInfo(@Valid UpdateUserInfoBO updateUserInfoBO, BindingResult result) {
        if (result.hasErrors()) {
            Map<String, String> map = ValidateUtils.checkParams(result);
            return Result.errorMap(map);
        }

        userService.updateUserInfo(updateUserInfoBO);

        return Result.ok();
    }

    @Override
    public Result queryByIds(String userIds) {
        if (StringUtils.isBlank(userIds)) {
            return Result.errorCustom(ResponseStatusEnum.USER_NOT_EXIST_ERROR);
        }

        List<AppUserVO> publisherList = new ArrayList<>();
        List<String> userIdList = JsonUtils.jsonToList(userIds, String.class);
        for (String userId : userIdList) {
            // 获得用户基本信息
            // 1. 根据userId查询用户的信息
            AppUser user = getUser(userId);

            // 2. 返回用户信息
            AppUserVO userVO = new AppUserVO();
            BeanUtils.copyProperties(user, userVO);
            // 添加到publisherList
            publisherList.add(userVO);
        }

        return Result.ok(publisherList);
    }

    @Override
    public Result getAccountInfo(String userId) {
        if (StringUtils.isBlank(userId)) {
            throw new HuaException(ResponseStatusEnum.UN_LOGIN);
        }
        // 1. 根据userId查询用户的信息
        AppUser user = getUser(userId);

        // 2. 返回用户信息
        UserAccountInfoVO accountInfoVO = new UserAccountInfoVO();
        BeanUtils.copyProperties(user, accountInfoVO);

        return Result.ok(accountInfoVO);
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    private AppUser getUser(String userId) {
        // 从缓存中获取用户数据
        String userInfoStr = redisUtils.get(RedisKeys.getUserInfoKey(userId));
        AppUser user = new AppUser();
        if (StringUtils.isNotBlank(userInfoStr)) {
            user = JsonUtils.jsonToPojo(userInfoStr, AppUser.class);
        } else {
            user = userService.selectUserById(userId);
            // 将数据放入缓存
            redisUtils.set(RedisKeys.getUserInfoKey(userId), JsonUtils.objectToJson(user));
        }
        return user;
    }
}
